package com.molyfun.parents.activity.record.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.record.itemview.BaseRecordView;
import com.molyfun.parents.activity.record.itemview.RecordItemView;
import com.molyfun.parents.rest.bean.RecordResponse;
import com.molyfun.parents.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 */
public class RecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements BaseRecordView.RecordOperationCallBack {

    //    private static final int TYPE_HEADER = 1001;
//    private static final int TYPE_CATEGORY = 1002;
    private static final int FIRST_POSITION = 0;
    private Context mContext;

    private List<RecordResponse.RecordBean> mRecordList;

    public RecordAdapter(Context mContext, RecordAdapterCallback recordAdapterCallback) {
        this.mContext = mContext;
        this.mRecordAdapterCallback = recordAdapterCallback;
    }

    public HashMap<String, Integer> mShowDatePosition = new HashMap<>();

    public String getDateByPosition(int position) {
        if (mShowDatePosition != null && mRecordList != null && mRecordList.size() > 0 && mRecordList.size() > position) {
            return TimeUtils.getYearForDate(mRecordList.get(position).getCreatedatetime());
        }
        return "";
    }

    public void deleteRecordBean(RecordResponse.RecordBean recordBean) {
        int position = mRecordList.indexOf(recordBean);
        String deleteDateStr = TimeUtils.transForDate(recordBean.getCreatedatetime());
        mRecordList.remove(position);
        if (mShowDatePosition.get(deleteDateStr) == position) {
            mShowDatePosition.remove(deleteDateStr);
        }
        //当记录数量大于position的时候才有必要处理后面的数据
        if (mRecordList.size() >= position) {
            for (int pointPosition = position; pointPosition < mRecordList.size(); pointPosition++) {
                RecordResponse.RecordBean currentPointRecordBean = mRecordList.get(pointPosition);
                String currentPointDateStr = TimeUtils.transForDate(currentPointRecordBean.getCreatedatetime());
                if (!mShowDatePosition.containsKey(currentPointDateStr)) {
                    mShowDatePosition.put(currentPointDateStr, pointPosition);
                } else {
                    if (mShowDatePosition.get(currentPointDateStr) == (pointPosition + 1)) {
                        mShowDatePosition.put(currentPointDateStr, pointPosition);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

//    public void addPlaceHolder(RecordResponse.PlaceHolderRecordBean placeHolderRecordBean) {
//        if(placeHolderRecordBean != null){
//
//        }
//    }
//
//    private List<RecordResponse.PlaceHolderRecordBean> placeHolderRecordBeanList = new LinkedList<>();
//
//    //发布预占位数据
//    public void addPlaceHolderData(RecordResponse.PlaceHolderRecordBean placeHolderRecordBean){
//
//        if(placeHolderRecordBean != null){
//            placeHolderRecordBeanList.add(placeHolderRecordBean);
//            addData(placeHolderRecordBean);
//        }
//    }
//
    public void updatePlaceHolderData(String path, RecordResponse.RecordBean recordBean){
        for(int position = 0; position < mRecordList.size(); position ++){
            RecordResponse.RecordBean recordBean1 = mRecordList.get(position);

            if((recordBean1 instanceof RecordResponse.PlaceHolderRecordBean) && path.equals(((RecordResponse.PlaceHolderRecordBean) recordBean1).getPath())){
                mRecordList.set(position, recordBean);
                notifyItemChanged(position);
                return;
            }


//            if(recordBean1 instanceof RecordResponse.PlaceHolderRecordBean){
//                //要更新状态，应该是失败了
//            }else{
//                if(path.equals(((RecordResponse.PlaceHolderRecordBean) recordBean1).getPath())){
//                    mRecordList.set(position, recordBean);
//                    notifyItemChanged(position);
//                    return;
//                }
//            }
        }
    }

    public void addData(RecordResponse.RecordBean recordBean) {
        if (recordBean == null) {
            return;
        }
        if (mRecordList == null) {
            mRecordList = new ArrayList<>();
        }
        //如果日期和第二个相同，需要处理日期
        if (mRecordList.size() > 0) {
            for (String key : mShowDatePosition.keySet()) {
                mShowDatePosition.put(key, (mShowDatePosition.get(key) + 1));
            }
        }
        mShowDatePosition.put(TimeUtils.transForDate(recordBean.getCreatedatetime()), FIRST_POSITION);

        mRecordList.add(FIRST_POSITION, recordBean);
        notifyDataSetChanged();
    }

    //加载更多，放到队列最后
    public void addAll(List<RecordResponse.RecordBean> list) {
        if (mRecordList == null) {
            mRecordList = new ArrayList<>();
        }
        preHandleDate(list);
        mRecordList.addAll(list);
        notifyDataSetChanged();
    }

    public RecordResponse.RecordBean getData(int position) {
        return mRecordList.get(position);
    }

    //加载更多，放到队列最后
    public void addAllHeader(List<RecordResponse.RecordBean> list) {
        if (mRecordList == null) {
            mRecordList = new ArrayList<>();
        }
        preHandleDate(list);
        mRecordList.addAll(0, list);
        notifyDataSetChanged();
    }

    public String getFirstTimestamp() {
        if (mRecordList != null) {
            if (mRecordList.size() > 0) {
                Iterator<RecordResponse.RecordBean> recordIterator = mRecordList.iterator();
                while(recordIterator.hasNext()){
                    RecordResponse.RecordBean recordBean = recordIterator.next();
                    if(!TextUtils.isEmpty(recordBean.getId())){
                        return recordBean.getCreatedatetime() + "";
                    }
                }

            }
        }
        return null;
    }

    public String getLastTimestamp() {
        if (mRecordList != null) {
            if (mRecordList.size() > 0) {
                return mRecordList.get(mRecordList.size() - 1).getCreatedatetime() + "";
            }
        }
        return null;
    }

    public boolean hasData() {
        if (mRecordList != null && mRecordList.size() > 0) {
            return true;
        }
        return false;
    }


    public void refreshData(List<RecordResponse.RecordBean> list) {
        mShowDatePosition.clear();
        preHandleDate(list);
        this.mRecordList = list;
        notifyDataSetChanged();
    }

    private void preHandleDate(List<RecordResponse.RecordBean> list) {
        int position = 0;
        for (RecordResponse.RecordBean recordBean : list) {
            String date = TimeUtils.transForDate(recordBean.getCreatedatetime());
            if (!mShowDatePosition.containsKey(date)) {
                if (mRecordList != null) {
                    mShowDatePosition.put(date, mRecordList.size() + position);
                } else {
                    mShowDatePosition.put(date, position);
                }
            }
            position++;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HomeWorkViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_record_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HomeWorkViewHolder homeWorkViewHolder = (HomeWorkViewHolder) holder;
        homeWorkViewHolder.mRecordItemView.initData(mRecordList.get(position), mShowDatePosition.containsValue(position), this);
    }


    @Override
    public int getItemCount() {
        if (mRecordList == null) {
            return 0;
        } else {
            return mRecordList.size();
        }
    }


    class HomeWorkViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.record_layout)
        RecordItemView mRecordItemView;

        public HomeWorkViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

    }

    @Override
    public void onRecordDel(RecordResponse.RecordBean recordBean) {
        deleteRecordBean(recordBean);
    }

    @Override
    public void onRecordReUpload(RecordResponse.RecordBean recordBean) {
        if(mRecordAdapterCallback != null){
            mRecordAdapterCallback.onRecordReupload(recordBean);
        }
    }

    private RecordAdapterCallback mRecordAdapterCallback;

    public interface RecordAdapterCallback{
        void onRecordReupload(RecordResponse.RecordBean recordBean);
    }

}
